package com.example.demo.thread;

/**
 * @Description:线程停止的demo
 * @Author: xiatian
 * @CreateTime: 2021/7/22
 * @Company: kangxu
 */
public class ThreadStopDemo extends Thread {

    private volatile boolean exists = false;

    @Override
    public void run() {
        int i = 0;
        while (!exists) {
            if (i < 5) {
                try {
                    i++;
                    System.out.println("开始睡眠2秒。。。");
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    break;
                }
            }else {
                exists = true;
            }
        }
    }


    public static void main(String[] args) {
        ThreadStopDemo tread = new ThreadStopDemo();
        tread.start();
    }
}
